#coding:utf8
import thread
import time
import os

tmp_file = 'temp.file.txt'
max_cpu = 0.0
command_top = 'top -b -n 1 > ' + tmp_file
command_top_threads = 'top -b -n 1 -Hp '

def get_top_snapshot():
    os.system(command_top)
    fo = open(tmp_file, "r")
    arr = fo.readlines()
    fo.close()
    return arr

def write_threads_of_pid(pid):
    os.system(command_top_threads + pid + ' > all_of_result_of_thread.txt')

def write_result_of_max_cpu(fc):
    fo = open("processes.log.txt", "w")
    fo.write(fc)
    fo.close()

def write_max_cpu(fc):
    fo = open("max.cpu.txt", "w")
    fo.write(fc)
    fo.close()

def write_result_of_max_cpu(fc):
    fc = fc +  time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())
    fo = open("result_of_max_cpu.txt", "w")
    fo.write(fc)
    fo.close()

def write_threads_info(_pid):
    cmd = 'jstack ' + _pid + ' > result_of_thread.txt'
    print 'start writing thread infos=' + cmd
    os.system(cmd)

    cmd = 'echo ' + time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) + ' >> result_of_thread.txt'
    os.system(cmd)

def get_index(arr, p):
    for i in range(len(arr)):
        if arr[i]==p:
            return i
    return 0
def get_max_cpu(now_result_arr):
    count = len(now_result_arr)

    title = now_result_arr[6]
    first_line = now_result_arr[7]
    
    title_line_words = title.split()
    first_line_words = first_line.split()

    pid_index = get_index(title_line_words, 'PID')
    user_name_index = get_index(title_line_words, 'USER')
    cpu_index = get_index(title_line_words, '%CPU')
    mem_index = get_index(title_line_words, '%MEM')
    process_name_index = get_index(title_line_words, 'COMMAND')

    print 'old pid=' + first_line_words[pid_index]
    print first_line_words[pid_index].isdigit()

    if not first_line_words[pid_index].isdigit():
        pid_index = pid_index - 1
        user_name_index = user_name_index - 1
        cpu_index = cpu_index - 1
        mem_index = mem_index - 1
        process_name_index = process_name_index - 1

    pid = first_line_words[pid_index]
    user_name = first_line_words[user_name_index]
    cpu = first_line_words[cpu_index]
    mem = first_line_words[mem_index]
    process_name = first_line_words[process_name_index]
    
    print 'now cpu=' + cpu + ', mem=' + mem + ',pid=' + pid + ', process name=' + process_name

    if cpu:
        return float(cpu), pid, process_name
    
    return 0, 1, ''

if __name__ == '__main__':
    while(1):
        try:
            now_result_arr = get_top_snapshot()
            now_cpu, now_pid, name = get_max_cpu(now_result_arr)
            if now_cpu > max_cpu:
                max_cpu = now_cpu
                write_max_cpu(str(max_cpu))
                write_result_of_max_cpu('\n'.join(now_result_arr))
                if name.index('java') > -1:
                    write_threads_info(now_pid)
                    write_threads_of_pid(now_pid)
                
                print 'max cpu changed to ' + str(max_cpu) + ',name=' + name
        except Exception,err:
            print err
        
        print 'will sleep for 1 second'
        time.sleep(1)